app.controller('userManage', ['$scope','$http','$rootScope', '$state','$sce','$document','$sessionStorage','$modal',function($scope,$http,$rootScope,$state,$sce,$document,$sessionStorage,$modal){
	// 获取用户列表
	$scope.getUserList=function(){
		var data={
			page_size:15,
			page:1
		}
		$http({
		    url:$rootScope.baseUrl+"UserManage/getUserList",
		    method:"post",
		    data:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.userList=res.data.data.list;

		    	if($scope.userList.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                method:"post",
		    	                url:$rootScope.baseUrl+'UserManage/getUserList',
		    	                data:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	$scope.userList=res.data.data.list;
		    	                	if($scope.userList.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getUserList();

	$scope.searchKey=[
		{
			"name":"用户账号",
			"id":"1"
		},
		{
			"name":"车牌号",
			"id":"2"
		}
	];
	$scope.nowSearchKey=$scope.searchKey[0];
	$scope.selectSearchKey=function(item){
		$scope.nowSearchKey=item;
		if($scope.keyword){
			if($scope.keyword.length!=0){
				$scope.searchUserInfo();
			}
		}
	}

	// 获取用户搜索列表
	$scope.searchUserInfo=function(){
		var data={
			type:$scope.nowSearchKey.id
		}
		if($scope.keyword){
			if($scope.keyword.length!=0){
				data.search=$scope.keyword;

				$http({
				    url:$rootScope.baseUrl+"UserManage/searchUserInfo",
				    method:"post",
				    data:data
				}).then(function(res){
				    if(res.data.code==200){
				    	$scope.userList=res.data.data.list;

				    	if($scope.userList.length==0){
				    		$scope.noData=true;
				    	}else {
				    		$scope.noData=false;
				    	}

				    	// 显示分页
				    	var total=res.data.data.total;
				    	createPage(15,8,total);//每页15条数据，显示8个按钮
				    	function createPage(pageSize, buttons, total) {
				    	    $(".pagination").jBootstrapPage({
				    	        pageSize : pageSize,
				    	        total : total,
				    	        maxPageButton:buttons,
				    	        onPageClicked: function(obj, pageIndex) {
				    	            data.page=pageIndex+1;
				    	            $http({
				    	                method:"post",
				    	                url:$rootScope.baseUrl+'UserManage/searchUserInfo',
				    	                data:data
				    	            }).then(function(res){
				    	                if(res.data.code==200){
				    	                	$scope.userList=res.data.data.list;
				    	                	if($scope.userList.length==0 && Math.ceil(total/15)==1){
				    	                		$scope.noData=true;
				    	                	}else {
				    	                		$scope.noData=false;
				    	                	}
				    	                }
				    	            },function(error){
				    	                console.log(error);
				    	            });
				    	        }
				    	    });
				    	}
				    }else if(res.data.code==3002){
				    	$scope.userList=res.data.data.list;
				    	$scope.noData=true;

				    	var total=res.data.data.total;
				    	createPage(15,8,total);//每页15条数据，显示8个按钮
				    	function createPage(pageSize, buttons, total) {
				    	    $(".pagination").jBootstrapPage({
				    	        pageSize : pageSize,
				    	        total : total,
				    	        maxPageButton:buttons,
				    	        onPageClicked: function(obj, pageIndex) {
				    	            
				    	        }
				    	    });
				    	}
				    }else if(res.data.code==409){
				    	$state.go("access.signin");
				    }
				},function(error){
				    console.log(error);
				});
			}
		}else {
			$scope.getUserList();
		}
	}

	$scope.clickInfo=function(carNum){
		if(carNum){
			var modalInstance = $modal.open({
			    templateUrl : 'carInfo.html',  //指向创建的视图
			    controller : 'carInfo',// 初始化模态范围
			    backdrop:"static"
			});
			
			$rootScope.carNum=carNum;
		}
	}
}]);

// 查看车牌信息
app.controller('carInfo', ['$scope','$http','$rootScope','$modalInstance',function($scope,$http,$rootScope,$modalInstance){
	$scope.cancel=function(){
		$modalInstance.close();
	}

	$http({
	    url:$rootScope.baseUrl+"UserManage/clickInfo",
	    method:"post",
	    data:{
	    	car_no:$rootScope.carNum
	    }
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.carDetail=res.data.data;
	    	console.log($scope.carDetail);
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});
}]);